package com.example.demo.controller;

import com.example.demo.entity.UserEntity;
import com.example.demo.jpa.UserJpa;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author rqian
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserJpa                       mUserJpa;
    @Autowired
    private RedisTemplate<Object, Object> objectRedisTemplate;



    @RequestMapping(value = "/list")
    public String list(Model model) {
        List<UserEntity> allUsers = (List<UserEntity>) objectRedisTemplate.opsForValue().get("users");
        if (CollectionUtils.isEmpty(allUsers)) {
            allUsers = mUserJpa.findAll();
            objectRedisTemplate.opsForValue().set("users", allUsers, 1, TimeUnit.MINUTES);
        }
        model.addAttribute("users", allUsers);
        return "userList";
    }

}
